package cn.source.web.controller.zk;

import cn.source.zk.domain.entity.BaseData;
import cn.source.zk.domain.entity.SimCard;
import cn.source.zk.service.IDbBaseDataService;
import cn.source.zk.service.IZkChannelService;
import cn.source.zk.service.IZkDeviceService;
import cn.source.zk.service.IZkSimCardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Component("timingTask")
@Slf4j
public class ZkTimeTasks {

    @Autowired
    private IDbBaseDataService baseDataService;

    @Autowired
    private IZkChannelService channelService;

    @Autowired
    private IZkDeviceService deviceService;

    @Autowired
    private IZkSimCardService simCardService;

    @Transactional
    public void task2BaseData() {
        log.info("测试任务：task2BaseData");
        System.out.println("测试定时任务。。。。。");
        ArrayList<String> deviceIds = new ArrayList<>();
        // 查询出两个小时内没有更新的通道；
        List<BaseData> baseDataList = baseDataService.checkBaseData();
        for (BaseData baseData : baseDataList) {
            // 查询通道对应的设备
            String zhGuid = channelService.getDeviceByTdGuid(baseData.getTdGuid());
            if (zhGuid != null){
                deviceIds.add(zhGuid);
            }
        }
        log.info("设备ID列表：{}",deviceIds);
        // 批量修改对应设备的状态
        if(deviceIds.size() > 0){
            deviceService.editBatchStates(deviceIds);
        }
    }

    /**
     * TODO 检测sim流量到期时间
     */
    @Transactional
    public void task2SimCardAlarm(){
        // 查询超过15内要到期的电话卡
        List<SimCard> SimCards = simCardService.getExpiringData();

        for (SimCard simCard : SimCards) {
            //将simCard信息写入到告警表中；

        }
    }
}
